package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.IIndexList;
import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IGridModel;
import org.rcgwt.client.ui.grid.internal.model.IGroupModel;
import org.rcgwt.client.ui.grid.internal.model.IGroupTitleModel;
import org.rcgwt.client.ui.grid.internal.model.IRowModel;
import org.rcgwt.client.ui.grid.internal.model.IStateMasks;
import org.rcgwt.client.ui.grid.model.IGridWidget;
import org.rcgwt.client.ui.grid.model.IGroup;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class GroupModel extends AbstractElementModel implements IGroupModel {

    private IGroupTitleModel[] groupTitleModels;

    private final IGridModel gridModel;

    private final IGroup group;

    public GroupModel(IGridModel gridModel, IGroup group, int modelIndex) {
        set(modelIndex, 0);

        this.gridModel = gridModel;

        this.group = group;
    }

    @Override
    public Type getType() {
        return Type.GROUP;
    }

    public void setGroupTitleModels(IGroupTitleModel groupTitleModels[]) {
        this.groupTitleModels = groupTitleModels;
    }

    @Override
    public String toString() {
        return "[Group modelIndex=" + modelIndex + " state=0x"
                + Integer.toHexString(stateMask) + "]";
    }

    @Override
    public IElementModel getParent() {
        return gridModel;
    }

    @Override
    public IGridModel getGridModel() {
        return gridModel;
    }

    @Override
    public IGroup getGroup() {
        return group;
    }

    @Override
    public IGroupTitleModel[] listGroupTitleModels() {
        return groupTitleModels;
    }

    @Override
    public IRowModel getRowModel(int rowModelIndex) {

        IRowModel rowModel = gridModel.searchRowModel(rowModelIndex);
        if (rowModel != null) {
            return rowModel;
        }

        int stateMask = computeRowStateMask(rowModelIndex);

        RowModel rowModel2 = new RowModel(rowModelIndex, stateMask, this);

        return rowModel2;
    }

    @Override
    public IRowModel setupSharedRowModel(int rowModelIndex) {

        RowModel rowModel = RowModel.sharedRowModel;

        int stateMask = computeRowStateMask(rowModelIndex);

        rowModel.set(rowModelIndex, stateMask, this);

        return rowModel;
    }

    protected int computeRowStateMask(int rowModelIndex) {
        int mask = 0;

        IGridWidget grid = getGridModel().getGridWidget();
        if (grid.isSelectable()) {
            IIndexList indexList = grid.getSelectionProvider()
                    .getSelectionList();

            if (indexList.contains(rowModelIndex)) {
                mask |= IStateMasks.SELECTED_STATE_MASK;
            }
        }
        if (grid.isCheckable()) {
            IIndexList indexList = grid.getCheckProvider().getCheckedList();

            if (indexList.contains(rowModelIndex)) {
                mask |= IStateMasks.CHECKED_STATE_MASK;
            }
        }

        return mask;
    }
}